package com.oneplatform.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeesuite.common.util.AssertUtil;
import com.jeesuite.common.util.BeanUtils;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.mybatis.plugin.pagination.PageParams;
import com.jeesuite.mybatis.plugin.pagination.PageExecutor;
import com.jeesuite.mybatis.plugin.pagination.PageExecutor.PageDataLoader;
import com.oneplatform.cms.dto.param.CmsArticleQueryParam;
import com.oneplatform.cms.dao.entity.CmsArticleEntity;
import com.oneplatform.cms.dao.mapper.CmsArticleEntityMapper;

/**
 * generated by www.jeesuite.com
 */
@Service
public class CmsArticleService {

	@Autowired
	private CmsArticleEntityMapper cmsArticleMapper;

	public void addCmsArticle(CmsArticleEntity entity) {
		cmsArticleMapper.insertSelective(entity);
	}

	public void updateCmsArticle(CmsArticleEntity entity) {
		CmsArticleEntity originEntity = cmsArticleMapper.selectByPrimaryKey(entity.getId());
		AssertUtil.notNull(originEntity);
		BeanUtils.copy(entity, originEntity);
		cmsArticleMapper.updateByPrimaryKey(originEntity);
	}
	
	public void deleteCmsArticle(Integer id) {
		cmsArticleMapper.deleteByPrimaryKey(id);
	}
	
	public CmsArticleEntity findCmsArticleById(Integer id){
		CmsArticleEntity entity = cmsArticleMapper.selectByPrimaryKey(id);
		AssertUtil.notNull(entity);
		return entity;
	}
	
    public Page<CmsArticleEntity> pageQuery(PageParams pageParam,CmsArticleQueryParam param){
		Page<CmsArticleEntity> page = PageExecutor.pagination(pageParam, new PageDataLoader<CmsArticleEntity>() {
			@Override
			public List<CmsArticleEntity> load() {
				return cmsArticleMapper.findListByQueryParam(param);
			}
		});
		return page;
	}

}
